package com.bsh.repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.bsh.mybatis.IRepository.AccountRefundMapper;
import com.bsh.mybatis.model.AccountRefundWithBLOBs;
import com.bsh.utils.JdbcHelper;
import com.bsh.utils.Pager;
import com.mysql.jdbc.StringUtils;

/**
 * 提现申请
 * 
 * @author gsh
 *
 */
@Repository
public class DaoAccountRefund {
	@Resource
	SqlSessionTemplate session;

	@Resource
	JdbcTemplate jdbcTemplate;
	@Resource
	NamedParameterJdbcTemplate namedJdbcTemplate;
	@Resource
	JdbcHelper jdbcHelper;

	AccountRefundMapper getMapper() {
		return session.getMapper(AccountRefundMapper.class);
	}

	public int deleteByPrimaryKey(Integer id) {
		int res = getMapper().deleteByPrimaryKey(id);
		return res;
	}

	public int insert(AccountRefundWithBLOBs record) {
		int res = getMapper().insert(record);
		return record.getId();
	}

	public int insertSelective(AccountRefundWithBLOBs record) {
		int res = getMapper().insertSelective(record);
		return record.getId();
	}

	public AccountRefundWithBLOBs selectByPrimaryKey(Integer id) {
		AccountRefundWithBLOBs row = getMapper().selectByPrimaryKey(id);
		return row;
	}

	public int updateByPrimaryKeySelective(AccountRefundWithBLOBs record) {
		int res = getMapper().updateByPrimaryKeySelective(record);
		return res;
	}

	public int updateByPrimaryKey(AccountRefundWithBLOBs record) {
		int res = getMapper().updateByPrimaryKey(record);
		return res;
	}

	/**
	 * 会员提现申请列表分页
	 */
	public List<Map<String, Object>> getCashPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("select a.*,u.name user_name,ub.bank_name,ub.bank_card,ub.bank_zone,ub.identify_name");
		sb.append(" from account_refund a");
		sb.append(" join user u on u.id=a.userid");
		sb.append(" join user_bank ub on ub.userid=a.userid and ub.id=a.user_bank_id");
		sb.append(" where 1=1 ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);
		BigDecimal money = jdbcHelper.scalarBigDecimal("select sum(money) from (" + sb.toString() + ")v", null);
	
		// sb.append(" order by a.audit_status,a.id desc ");
		sb.append(" order by a.id desc ");
		sb.append(" limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);

		pager.setRowCount(count);
		pager.putData("sum_money", money);
        return list;
	}

	/**
	 * 会员提现申请列表分页
	 */
	public List<Map<String, Object>> getCashPageExport(String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append("select a.*,u.name user_name,ub.bank_name,ub.bank_card,ub.bank_zone,ub.identify_name");
		sb.append(",DATE_FORMAT(a.create_time,'%Y-%m-%d %H:%i') create_time_ch");
		sb.append(",DATE_FORMAT(a.audit_time,'%Y-%m-%d %H:%i') audit_time_ch");
		sb.append(",case a.audit_status");
		sb.append(" WHEN 0 then '待审核'");
		sb.append(" WHEN 1 then '已通过'");
		sb.append(" WHEN 2 then '未通过'");
		sb.append(" WHEN 3 then '已打款'");
		sb.append("end audit_status_ch ");
		sb.append(" from account_refund a");
		sb.append(" join user u on u.id=a.userid");
		sb.append(" join user_bank ub on ub.userid=a.userid and ub.id=a.user_bank_id");
		sb.append(" where 1=1 ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		sb.append(" order by a.id desc ");
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);
		return list;
	}

	/**
	 * 审核
	 * 
	 * @param userid
	 * @return
	 */
	public Integer audit(int askId, boolean isPass, String auditMsg) {
		AccountRefundMapper map = this.getMapper();
		if (isPass)
			return map.auditPass(askId, new Date());
		else
			return map.auditRefuse(askId, auditMsg, new Date());
	}

	/**
	 * 撤消审核
	 * 
	 * @param userid
	 * @return
	 */
	public Integer auditCancel(int askId) {
		AccountRefundMapper map = this.getMapper();
		return map.auditCancel(askId);
	}

	/**
	 * 审核确认打款
	 * 
	 * @param userid
	 * @return
	 */
	public Integer auditPay(int askId, String payMsg) {
		AccountRefundMapper map = this.getMapper();
		return map.auditPay(askId, payMsg);
	}

	/**
	 * 提现列表查询
	 * 
	 * @param pager
	 * @param where
	 * @return
	 */
	public List<Map<String, Object>> getWithDrawalPage(Pager pager, String where) {
		StringBuilder sb = new StringBuilder();
		MapSqlParameterSource ps = new MapSqlParameterSource();
		sb.append(" SELECT t1.userid,t1.account_id,t1.money,t2.bank_card,t2.bank_zone,t3.name,t1.memo,t1.audit_status,t1.pay_time,t1.create_time AS apply_time from account_refund t1");
		sb.append(" LEFT JOIN user_bank t2 on t1.user_bank_id=t2.id");
		sb.append(" LEFT JOIN bank t3 on t2.bankid=t3.id");
		sb.append(" where 1=1 ");
		if (!StringUtils.isNullOrEmpty(where)) {
			sb.append(where);
		}
		Integer count = namedJdbcTemplate.queryForInt("select count(0) from (" + sb.toString() + ")v", ps);
		sb.append(" order by t1.create_time desc ");
		sb.append(" limit :start,:size");
		ps.addValue("start", pager.getStart());
		ps.addValue("size", pager.getPageSize());
		List<Map<String, Object>> list = namedJdbcTemplate.queryForList(sb.toString(), ps);
		pager.setRowCount(count);
		return list;
	}

}
